// test_plugin_data_fix.js
// 测试插件数据接收接口修复

const axios = require('axios');

// 模拟银行流水数据
const bankData = {
  "batchId": "batch_test_fix_" + Date.now(),
  "timestamp": new Date().toISOString(),
  "dataType": "bank",
  "data": [{
    "url": "/uc-server/service/hzDealDetailReport/hzDealDetailQuery",
    "method": "POST",
    "timestamp": new Date().toISOString(),
    "response": {
      "success": true,
      "errorCode": "0",
      "msg": "成功",
      "data": {
        "messageTypeId": "0000",
        "transactionCode": "CCM02001",
        "receiverNode": "",
        "receiverServer": "",
        "receiverJournalNo": "20251124EA000000004754266604",
        "receiverDate": "2025-11-24",
        "receiverTime": "09:53",
        "transactionRef": "00262025112487262643",
        "destinationChannel": "",
        "businessCycle": 0,
        "businessDate": "",
        "relatedCycleNo": 0,
        "relatedAccDate": "",
        "statusCode": "000000",
        "statusType": "",
        "statusDescription": "",
        "hostName": "ueccashserver5",
        "threadNo": "231",
        "sourceSys": "",
        "targetSys": "",
        "systemLevel": "",
        "transDesc": "",
        "state": "N",
        "files": [],
        "filesMap": null,
        "checkToken": "",
        "responseFlowNo": "2025112413736536",
        "currentPage": 1,
        "pageSize": 50,
        "totalPageNumber": 1,
        "totalNumber": 8,
        "data": [
          {
            "orderFlowNo": "20251028R0841635",
            "debitAmount": "1000.00",
            "loanAmount": "0.00",
            "balance": "1076.23",
            "currencyType": "00",
            "currencyTypeHZ": "人民币",
            "currencyType1": "00",
            "currencyType1HZ": "人民币",
            "transferDate": "2025-10-28",
            "transferTime": "13:41:07",
            "summaryNo": "293",
            "summary": "网转",
            "vouchType": "2025-10-28",
            "vouchersNo": "",
            "recAccount": "3301040160019906307",
            "recAccountName": "聚采（杭州）供应链有限公司",
            "recAccountOpenBank": "杭州银行股份有限公司",
            "tranFee": "0.00",
            "transferNode": "",
            "transferNodeHZ": "",
            "organName": "杭州银行股份有限公司",
            "memo": "短期借款",
            "hostFlowNo": "202510287060846333",
            "recAccountOpenNode": "",
            "recBankType": "00",
            "bocFlag": "0",
            "busTime": "",
            "channelCode": "0026",
            "subAccountSeq": ""
          }
        ],
        "accountNo": "3301040160020881390",
        "accountName": "杭州银行三墩支行",
        "accAlias": "",
        "flag": "0",
        "pageTotal": "8",
        "debitTotal": "122512.00",
        "loanTotal": "90940.00",
        "debitPageTotal": "122512.00",
        "loanPageTotal": "90940.00",
        "subAccountSeq": "",
        "inTotal": "5",
        "outTotal": "3",
        "complete": true,
        "timestamp": "1763957222810",
        "message": "",
        "success": true,
        "code": "000000"
      }
    },
    "status": 200
  }]
};

// 模拟发票数据
const invoiceData = {
  "url": "/szzhzz/qlfpcx/v1/queryFpjcxx?6eMrZlPH=adb520869482ca2607dcd87050f8e20d7c2dcf4cc356575e16a96eb4baf6e7aa55c9c6a9da902bb2b5930373073954bcf55cdb5641a90d4d5ed85be281b97d26c06414bda00b3ffeef02b9edfac483adde5b7a246f571e64880e5517c611a2ad709cd2755c1933838316e029f76aec34",
  "method": "POST",
  "timestamp": "2025-11-23T14:26:41.791Z",
  "response": {
    "Response": {
      "RequestId": "8db9a892e8ef5544",
      "Data": {
        "PageNumber": 1,
        "PageSize": 20,
        "Total": 2,
        "List": [
          {
            "Gjbq": "1",
            "Fphm": "25332000000530054993",
            "Zzfphm": "",
            "ZzfpDm": "",
            "FplyDm": "2",
            "FplxDm": "81",
            "FppzDm": "01",
            "FpztDm": "01",
            "Sflzfp": "Y",
            "Kpfnsrsbh": "91330106MADX54C99B",
            "Xsfnsrsbh": "91330106MADX54C99B",
            "Xsfmc": "杭州科技有限公司",
            "Gmfnsrsbh": "91110108682896990A",
            "Gmfmc": "行丰有限公司",
            "Kprq": "2025-11-21 13:18:07",
            "Hjje": 9900.99,
            "Hjse": 99.01,
            "XsfssjswjgDm": "13300000000",
            "GmfssjswjgDm": "11100000000",
            "FpkjfsDm": "1",
            "Kpr": "徐根福",
            "Jshj": 10000,
            "Gmfzrrbz": "N",
            "Tdyslxmc": "",
            "Xsfssjswjgmc": "国家税务总局浙江省税务局"
          }
        ]
      }
    }
  },
  "status": 200
};

// 模拟插件数据格式（银行流水）
const pluginBankData = {
  batchId: "batch_test_bank_" + Date.now(),
  timestamp: new Date().toISOString(),
  dataType: "bank",
  data: [bankData]
};

// 模拟插件数据格式（发票）
const pluginInvoiceData = {
  batchId: "batch_test_invoice_" + Date.now(),
  timestamp: new Date().toISOString(),
  dataType: "invoice",
  data: [invoiceData]
};

async function testPluginDataReception() {
  console.log('🚀 开始测试插件数据接收接口修复...');
  
  try {
    // 测试银行流水数据发送
    console.log('\n1. 测试银行流水数据发送...');
    const bankResponse = await axios.post('http://127.0.0.1:9001/api/v1/plugin/data/receive', pluginBankData, {
      // 故意添加一个错误格式的token来测试修复效果
      headers: {
        'Authorization': '619c1f35-58e0-4c9f-b702-abfdd8c39e09'
      }
    });
    console.log('✅ 银行流水数据发送成功!');
    console.log('📊 响应:', bankResponse.data);
    
    // 测试发票数据发送
    console.log('\n2. 测试发票数据发送...');
    const invoiceResponse = await axios.post('http://127.0.0.1:9001/api/v1/plugin/data/receive', pluginInvoiceData, {
      // 故意添加一个错误格式的token来测试修复效果
      headers: {
        'Authorization': '619c1f35-58e0-4c9f-b702-abfdd8c39e09'
      }
    });
    console.log('✅ 发票数据发送成功!');
    console.log('📊 响应:', invoiceResponse.data);
    
    // 等待一段时间让数据处理完成
    console.log('\n3. 等待数据处理完成...');
    await new Promise(resolve => setTimeout(resolve, 3000));
    
    // 查询处理状态
    console.log('\n4. 查询处理状态...');
    const statusResponse = await axios.get(`http://127.0.0.1:9001/api/v1/plugin/data/batches`);
    console.log('📊 批次列表:', statusResponse.data);
    
    console.log('\n🎉 测试完成！插件数据接收接口修复成功！');
    
  } catch (error) {
    console.error('❌ 测试失败:', error.message);
    if (error.response) {
      console.error('📋 错误详情:', error.response.data);
    }
  }
}

// 执行测试
testPluginDataReception();